package com.wxq.schoolmap;

import com.wxq.draw.DrawDBTool;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddSpotActivity extends Activity {

	EditText spotIdET;
	EditText DisplayET;
	EditText CompanyNameET;
	EditText MapNameET;
	EditText TypeET;
	EditText AddressET;
	EditText PhoneET;
	EditText EmailET;
	EditText OpeningTimeET;
	EditText Coord_XET;
	EditText Coord_YET;
	
	
	Button getPicBtn;
	Button modifyBtn;
	Button cancelBtn;
	Button commitBtn;
	View modifyBar;
	
	Button gpsBtn;
	Button unitTypeBtn;
	
	String spotId ;
	String MallDBpath;
	DrawDBTool dbTool;
	
	String floorName;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.spot_detail);
		
		Intent intent = getIntent();
		MallDBpath = intent.getStringExtra("MallDBpath"); 
		
		//获取UUID
		spotId = java.util.UUID.randomUUID().toString();
		floorName = getIntent().getStringExtra("floorName");
		
		dbTool = new DrawDBTool(this);
		dbTool.setDBpath(MallDBpath);
		
		getAllViews();
		
		fillAllViews();
		
		//右侧按钮 == 提交功能
		modifyBtn.setText("提交");
		modifyBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				addSpot();
				Toast.makeText(AddSpotActivity.this, "添加热点成功！", Toast.LENGTH_LONG).show();
			}
		});
		
		//左侧按钮 == 取消功能
		getPicBtn.setText("取消");
		getPicBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
//		cancelBtn.setOnClickListener(new View.OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				// 取消修改
//
//				disableAllViews();
//				modifyBar.setVisibility(View.GONE);
//			}
//		});
//		
//		commitBtn.setOnClickListener(new View.OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				//提交修改
//				
//				disableAllViews();
//				modifyBar.setVisibility(View.GONE);
//			}
//		});

	}
	
	public void addSpot() {
		String spotUUID = spotId;
		String display = DisplayET.getText().toString();
		String companyName = CompanyNameET.getText().toString();
		String mapName = floorName;
		String type = TypeET.getText().toString();
		String address = AddressET.getText().toString();
		String phone = PhoneET.getText().toString();
		String email = EmailET.getText().toString();
		String openTime = OpeningTimeET.getText().toString();
		String coord_x = Coord_XET.getText().toString();
		String coord_y = Coord_YET.getText().toString();
		String polygon = getDefaultPolygon(Float.parseFloat(coord_x), Float.parseFloat(coord_y));
		
		//装填draw表信息。
		ContentValues drawValues = new ContentValues();
		drawValues.put("Type", 2+"");
		drawValues.put("Path", polygon);
		drawValues.put("RenderId", -1+"");
		drawValues.put("Mapid", mapName);
		drawValues.put("SpotId", spotUUID);
		drawValues.put("Visible", 1+"");
		drawValues.put("TextStr", "Shop");
		dbTool.addSpotToDraw(drawValues);
		
		//装填InfoTable表信息。
		ContentValues infoValues = new ContentValues();
		infoValues.put("SpotID", spotUUID);
		infoValues.put("CompanyName", companyName);
		infoValues.put("Display", display);
		infoValues.put("MapName", mapName);
		infoValues.put("Type", type);
		infoValues.put("Address", address);
		infoValues.put("Phone", phone);
		infoValues.put("Email", email);
		infoValues.put("OpeningTime", openTime);
		infoValues.put("Coord_X", coord_x);
		infoValues.put("Coord_Y", coord_y);
		infoValues.put("polygon", polygon);
		//infoValues.put("UnitTypeID", "1315");
		infoValues.put("Removed", "3");//新增
		dbTool.addSpotToInfoTable(infoValues);
		if (dbTool.isOutdoor()) {
			dbTool.addCoord2Outdoor(spotUUID);
		}
	}
	
	public String getDefaultPolygon(float x,float y) {
		float pWidth = 5.0f;
		float pHight = 5.0f;
		float xStep = pWidth/2.0f;
		float yStep = pHight/2.0f;
		return  	(x - xStep) + "," + (y - yStep) + ";" + //左上
					(x + xStep) + "," + (y - yStep) + ";" + //右上
					(x + xStep) + "," + (y + yStep) + ";" + //右下
					(x - xStep) + "," + (y + yStep) + ";" + //左下
					(x - xStep) + "," + (y - yStep) + ";" ; //左上
	}
	
	public void fillAllViews() {
		spotIdET.setEnabled(false);
		spotIdET.setText(spotId);
		MapNameET.setEnabled(false);
		MapNameET.setText(floorName);
		Coord_XET.setEnabled(false);
		Coord_XET.setText(getIntent().getStringExtra("X_Coord"));
		Coord_YET.setEnabled(false);
		Coord_YET.setText(getIntent().getStringExtra("Y_Coord"));
//		TypeET.setEnabled(false);
		TypeET.setText("商铺");
	}
	
	public void getAllViews() {
		//获取所有的控件
		getPicBtn = (Button) findViewById(R.id.getPicBtn);
		modifyBtn = (Button) findViewById(R.id.modifyBtn);
		modifyBar = findViewById(R.id.modifyBar);
		cancelBtn = (Button) findViewById(R.id.cancelBtn);
		commitBtn = (Button) findViewById(R.id.commitBtn);
		//内容视图
		spotIdET = (EditText) findViewById(R.id.spotIdET);
		DisplayET = (EditText) findViewById(R.id.DisplayET);
		CompanyNameET = (EditText) findViewById(R.id.CompanyNameET);
		MapNameET = (EditText) findViewById(R.id.MapNameET);
		TypeET = (EditText) findViewById(R.id.TypeET);
		AddressET = (EditText) findViewById(R.id.AddressET);
		PhoneET = (EditText) findViewById(R.id.PhoneET);
		EmailET = (EditText) findViewById(R.id.EmailET);
		OpeningTimeET = (EditText) findViewById(R.id.OpeningTimeET);
		Coord_XET = (EditText) findViewById(R.id.Coord_XET);
		Coord_YET = (EditText) findViewById(R.id.Coord_YET);
		gpsBtn = (Button) findViewById(R.id.gpsBtn);
		unitTypeBtn = (Button) findViewById(R.id.unitTypeBtn);
		gpsBtn.setVisibility(View.GONE);
		unitTypeBtn.setVisibility(View.GONE);
	}
	public void enableAllViews() {
	
		DisplayET.setEnabled(true);
		CompanyNameET.setEnabled(true);
		MapNameET.setEnabled(true);
		TypeET.setEnabled(true);
		AddressET.setEnabled(true);
		PhoneET.setEnabled(true);
		EmailET.setEnabled(true);
		OpeningTimeET.setEnabled(true);
		//Coord_XET.setEnabled(true);
		//Coord_YET.setEnabled(true);
	}
	
	public void disableAllViews() {
		spotIdET.setEnabled(false);
		DisplayET.setEnabled(false);
		CompanyNameET.setEnabled(false);
		MapNameET.setEnabled(false);
		TypeET.setEnabled(false);
		AddressET.setEnabled(false);
		PhoneET.setEnabled(false);
		EmailET.setEnabled(false);
		OpeningTimeET.setEnabled(false);
		Coord_XET.setEnabled(false);
		Coord_YET.setEnabled(false);
	}
	
	@Override
	protected void onDestroy() {
		dbTool.close();
		super.onDestroy();
	}
}
